package com.food.restaurantpro.observers;

import java.util.Observable;
import java.util.Observer;

import com.food.restaurantpro.RestaurantApp;
import com.food.restaurantpro.connection.DataHelper;
import com.food.restaurantpro.connection.DataResult;
import com.food.restaurantpro.connection.Urls;
import com.food.restaurantpro.log.Log;
import com.food.restaurantpro.managingnetwork.ManagingNetwork;
import com.food.restaurantpro.objects.CurrentTime;
import com.food.restaurantpro.utils.Prefs;

public class CurrentTimeObserver implements Observer {
	public CurrentTime mCurrentTime;
	
	private CurrentTimeObserver _me;
	private boolean hadRegisterObserver = false;
	
	private CurrentTimeObserverInterface mCurrentTimeObserverInterface;

	public CurrentTimeObserver(CurrentTimeObserverInterface currentTimeObserverInterface) {
		_me = this;
		mCurrentTimeObserverInterface = currentTimeObserverInterface;
	}
	
	public void registerObserver() {
		if (ManagingNetwork.isNetworkAvailable()) {
			hadRegisterObserver = true;
			// get current time
			DataHelper.getInstance().addObserver(_me);
			DataHelper.getInstance().getCurrentTime();
		} else {
			mCurrentTimeObserverInterface.currentTimeCallback(null);
		}
	}

	/**
	 * must call this method in onDestroy of Activity which called
	 * registerObserver
	 */
	public void unRegisterObserver() {
		if (hadRegisterObserver) {
			DataHelper.getInstance().deleteObserver(_me);
			hadRegisterObserver = false;
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// close progress
		if (data == null) {
			mCurrentTimeObserverInterface.currentTimeCallback(null);
			return;
		}
			
		DataResult result = (DataResult) data;
		switch (result.getMethodName()) {
		case getCurrentTime:
			if (result.getErrorCode() != null) {
				// show error code
				mCurrentTimeObserverInterface.currentTimeCallback(null);
			} else {
				mCurrentTime = (CurrentTime) result.getData();
				if(mCurrentTime != null) {
					Log.i("CurrentTimeObserver", "update() getCurrentTime",
							"Date = " + mCurrentTime.getDate() + ", Time = "
									+ mCurrentTime.getTime());
					//save CurrentTime to SharedPreferences
					Prefs pref = new Prefs(RestaurantApp.CURRENT_CONTEXT);
					pref.putStringValue(mCurrentTime.getDate(), Prefs.KEY_CURRENT_DAY);
					pref.putStringValue(mCurrentTime.getTime(), Prefs.KEY_CURRENT_TIME);
					mCurrentTimeObserverInterface.currentTimeCallback(mCurrentTime);
				} else {
					android.util.Log.e("CurrentTimeObserver", "Update Method return null when called " + Urls.LINK_TO_GET_CURRENT_TIME);
					mCurrentTimeObserverInterface.currentTimeCallback(null);
				}
			}
			unRegisterObserver();
			break;
		default:
			break;
		}
		// Chu y: Observer se post notification den tat ca cac lop da register
		// observer. Va cho nao co phuong thuc trung voi phuong thuc no tra ve thi xu ly
		// cho do, nhung cho khac ko xu ly, vi vay PHAI dat code o trong "case method:"
		// khong duoc dat o ngoai, nhu o day la "case getCurrentTime:"
	}
}
